import axios from "axios";
import { url } from "./url";

interface Project {
  id: string;
  image: string;
  name: string;
  createdAt: string;
  updatedAt: string;
}
interface ProjectResponse {
  data: any;
}
export const createProject = async (data: {
  image?: string;
  name: string;
  userId: string;
}): Promise<Project> => {
  return axios
    .post(url + "/api/project", data)
    .then((res: ProjectResponse) => res.data);
};

export const copyProject = async (id: string): Promise<Project> => {
  return axios
    .post(url + `/api/project/${id}`)
    .then((res: ProjectResponse) => res.data);
};

export const getAllProjects = async (params?: {
  size?: number;
  page?: number;
  search?: string;
  userId: string;
}): Promise<{
  data: Project[];
  total: number;
  current: number;
  size: number;
}> => {
  return axios
    .get(url + "/api/project", {
      params,
    })
    .then((res: ProjectResponse) => res.data);
};

export const getProjectById = async (id: string): Promise<Project> => {
  return axios
    .get(url + `/api/project/${id}`)
    .then((res: ProjectResponse) => res.data);
};

export const updateProject = async (data: {
  id: string;
  name?: string;
  image?: string;
  projectContent?: any;
  userId: string;
}): Promise<Project> => {
  const { id, ...updateData } = data;
  return axios
    .patch(url + `/api/project/${id}`, updateData)
    .then((res: ProjectResponse) => res.data);
};

export const deleteProject = async (id: string): Promise<any> => {
  return axios
    .delete(`/api/project/${id}`)
    .then((res: ProjectResponse) => res.data);
};
